import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component:()=>import('../views/Home/Home'),
    redirect:'/welcome',
    // 首页子路由
    children:[
      {
        path:"/welcome",
        component:()=>import('../components/Welcome')
      },
      {
        path:"users",
        component:()=>import('../views/Home/Users')
      },
      {
        path:"roles",
        component:()=>import('../views/Home/Roles')
      },
      {
        path:"rights",
        component:()=>import('../views/Home/Rights')
      },
      {
        path:"goods",
        component:()=>import('../views/Home/Goods')
      },
      {
        path:"params",
        component:()=>import('../views/Home/Params')
      },
      {
        path:"categories",
        component:()=>import('../views/Home/Categories')
      },
      {
        path:"orders", 
        component:()=>import('../views/Home/Orders')
      },
      {
        path:"reports",
        component:()=>import('../views/Home/Reports')
      },
      // 添加商品 路由
      {
        path:"goods/add",
        component:()=>import('../views/Home/Add')
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:'/login',
    name:'Login',
    component: () => import ('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫 登录验证
router.beforeEach((to,from,next)=>{
  // document.title = to.meta.title

  let token = sessionStorage.getItem("token")

  if(token){
    if(to.path === "/login"){
      next("/")
    }else{
      next()
    }
  }else{
    if(to.path == "/login"){
      next()
    }else{

      next("/login")
    }
  }
})

export default router
